package com.library.system.modules.emailconfig.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.library.common.enums.StatusEnum;
import com.library.common.util.EncryptUtil;
import com.library.system.modules.emailconfig.entity.EmailConfig;
import com.library.system.modules.emailconfig.vo.EmailConfigVO;
import com.library.system.modules.emailconfig.bo.EmailConfigPage;
import com.library.system.modules.emailconfig.bo.EmailConfigInsert;
import com.library.system.modules.emailconfig.bo.EmailConfigUpdate;
import com.library.system.modules.emailconfig.mapper.EmailConfigMapper;
import com.library.system.modules.emailconfig.struct.EmailConfigStructMapper;
import com.library.system.modules.emailconfig.service.EmailConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import com.library.common.util.PageCovertUtil;

import java.util.List;
import java.util.Random;


/**
 * @Author xyh
 * @Desc 邮箱配置(EmailConfig)表服务实现类
 * @Date 2023-09-24 16:14:28
 */

@Log4j2
@Service("emailConfigService")
public class EmailConfigServiceImpl extends ServiceImpl<EmailConfigMapper, EmailConfig> implements EmailConfigService {

    @Resource
    private EmailConfigStructMapper emailConfigStructMapper;
    @Resource
    private EmailConfigMapper emailConfigMapper;


    @Override
    public List<EmailConfigVO> emailConfigList() {
        List<EmailConfig> list = list();
        List<EmailConfigVO> emailConfigVOS = emailConfigStructMapper.configListToEmailConfigVO(list);
        if (CollUtil.isNotEmpty(emailConfigVOS)) {
            emailConfigVOS.forEach(v -> {
                v.setEmailStatusName(StatusEnum.getValue(v.getEmailStatus()));
                try {
                    v.setPass(EncryptUtil.desDecrypt(v.getPass()));
                } catch (Exception e) {
                    log.error("邮件配置密码还原失败：id为：{}", v.getId());
                }
            });
        }
        return emailConfigVOS;
    }


    @Override
    public EmailConfigVO queryById(Integer id) {
        EmailConfig emailConfig = baseMapper.selectById(id);
        return emailConfigStructMapper.emailConfigToEmailConfigVO(emailConfig);
    }

    @Override
    public boolean insert(EmailConfigInsert emailConfigInsert) {
        EmailConfig emailConfig = emailConfigStructMapper.insertToEmailConfig(emailConfigInsert);
        //加密
        try {
            emailConfig.setPass(EncryptUtil.desEncrypt(emailConfig.getPass()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        save(emailConfig);
        return true;
    }


    @Override
    public boolean update(EmailConfigUpdate emailConfigUpdate) {
        EmailConfig emailConfig = emailConfigStructMapper.updateToEmailConfig(emailConfigUpdate);
        //加密
        try {
            emailConfig.setPass(EncryptUtil.desEncrypt(emailConfig.getPass()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        updateById(emailConfig);
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        return removeById(id);
    }

    @Override
    public EmailConfig getSendEmail() {
        //查询状态为正常的配置
        List<EmailConfig> emailConfigs = lambdaQuery()
                .eq(EmailConfig::getEmailStatus, StatusEnum.NORMAL.getCode())
                .list();
        if (CollUtil.isNotEmpty(emailConfigs)) {
            Random random = new Random();
            // 生成一个随机索引
            int randomIndex = random.nextInt(emailConfigs.size());
            EmailConfig emailConfig = emailConfigs.get(randomIndex);
            return emailConfig;
        }
        return null;
    }

}
